import { withErrors, ok, apiError, parseJson } from "@/lib/api"; import { requireUser } from "@/lib/auth"; import { UpdateSource } from "@/lib/schemas/source"; import { getSource, updateSource } from "@/lib/store"; import { currentState, ensureFresh } from "@/lib/refresh"; // GET /api/sources/:id — cached state; ?fresh=1 refreshes if stale first. export const GET = withErrors(async (req, { params }) => { await requireUser(req); const { id } = await params; const source = getSource(id); if (!source) return apiError("not_found", "Source not found", 404); const fresh = new URL(req.url).searchParams.get("fresh") === "1"; const state = fresh ? await ensureFresh(source) : currentState(source); return ok({ source: state }); }); // PATCH /api/sources/:id — update label / enabled / config / refreshSeconds / position. export const PATCH = withErrors(async (req, { params }) => { await requireUser(req); const { id } = await params; if (!getSource(id)) return apiError("not_found", "Source not found", 404); const patch = await parseJson(req, UpdateSource); const source = updateSource(id, patch); return ok({ source }); });